#!/bin/sh
# see qemu-env in Makefile

echo "running $0"

if [ -z ${EELDIR} ]; then
    source $(dirname $0)/qemu-run.env
fi

if [ -z ${EELDIR} ]; then
    echo "no env found"
    exit 1
fi

# disable boottime trace, avoid trace buffer overflow
echo 0 > /sys/kernel/debug/tracing/tracing_on

mkdir -p ${EELDIR}
mount -t 9p -o ro mnt_eel ${EELDIR}

if [ -f /lib/modules/$(uname -r) ]; then
    rm -f /lib/modules/$(uname -r)
fi

MAPDIR=/mnt/arch/kernel/linux-${KL_VERSION}/${KL_CONFIG}/kmods/lib/modules/$(uname -r)
if [ -e ${MAPDIR} ]; then
    echo "mounting /lib/modules/$(uname -r)"
    mkdir -p /lib/modules/$(uname -r)
    mount --bind ${MAPDIR} /lib/modules/$(uname -r)
    \rm -fr /mnt/arch/kmods
    ln -sf ${MAPDIR} /mnt/arch/kmods
fi

# FIXME:
if ls /dev/tee* 1> /dev/null 2>&1; then
    \rm -fr /lib/optee_armtz
    ln -sf  /mnt/arch/optee/optee_examples-${OPTEE_VERSION}/lib/optee_armtz \
        /lib/optee_armtz

    if [ -e /mnt/arch/optee/optee_examples-${OPTEE_VERSION}/usr/lib/tee-supplicant/plugins ]; then
        mkdir -p /usr/lib/tee-supplicant/plugins;
        cp -fr /mnt/arch/optee/optee_examples-${OPTEE_VERSION}/usr/lib/tee-supplicant/plugins/* \
            /usr/lib/tee-supplicant/plugins;
    fi

    tee-supplicant -d
fi
